<template>
	<view class="page">
		<view class="body">				
			<!-- <view class="step-bar">
				<view class="fix-wrap">
					<view class="item active">
						<view class="icon iconfont">&#xe8cc;</view>
						<text class="text">基本信息</text>
					</view>
					<view class="item">
						<view class="icon iconfont f-s38">&#xe621;</view>
						<text class="text">上传合同</text>
					</view>
					<view class="item">
						<view class="icon iconfont">&#xec35;</view>
						<text class="text">阶段任务</text>
					</view>
					<view class="item">
						<view class="icon iconfont">&#xe659;</view>
						<text class="text">回款计划</text>
					</view>
					<view class="item">
						<view class="icon iconfont">&#xeb67;</view>
						<text class="text">发送审批</text>
					</view>
				</view>	
			</view> -->
			
			<view >  
				<template v-if="page=='add'||page=='edit'">
					<u--form :model="form" ref="uForm" class="form-wrap"> 						
						<u-form-item label="采购公司" labelWidth="160" prop="company_val_name" borderBottom="true" class="u-form-item" @click="showCompany">
							<u-input v-model="form.company_val_name" disabled disabledColor="#ffffff" border="none" placeholder="选择采购公司" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>		
						</u-form-item>
						<!-- <u-form-item label="申请部门" labelWidth="160" prop="company_val_name" borderBottom="true" class="u-form-item">
							<u-input v-model="form.department_name" disabled disabledColor="#ffffff" border="none" placeholder="选择采购公司" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>		
						</u-form-item> -->
						<u-form-item label="申请原因或用途" labelWidth="240" prop="reason_purpose" borderBottom="true" class="u-form-item">
							<u-input v-model="form.reason_purpose" disabledColor="#ffffff" border="none" placeholder="选择申请原因或用途"/> 						
						</u-form-item> 
						
						<!-- <u-form-item label="部门" labelWidth="160" prop="department_name" borderBottom="true" class="u-form-item">
							<u-input v-model="form.department_name" disabled disabledColor="#ffffff" border="none" placeholder="选择部门" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>		
						</u-form-item> -->
						 
						<!-- <u-form-item label="状态" labelWidth="300" prop="status" borderBottom="true" class="u-form-item">
							<u--text v-if="status==1" text="提出采购申请" size="30" class="orange"></u--text>
							<u--text v-if="status==2" text="采购立项审批" size="30" class="blue"></u--text>									 
							<u--text v-if="status==3" text="提交采购计划" size="30" class="blue"></u--text>
							<u--text v-if="status==4" text="采购计划审批" size="30" class="blue"></u--text>
							<u--text v-if="status==5" text="拟定合同" size="30" class="blue"></u--text>
							<u--text v-if="status==6" text="合同审批" size="30" class="blue"></u--text>
							<u--text v-if="status==7" text="采购中" size="30" class="blue"></u--text>
							<u--text v-if="status==8" text="验收入库" size="30" class="green"></u--text> 
							<u--text v-if="status==9" text="货款核销" size="30" class="blue"></u--text>
							<u--text v-if="status==10" text="结束" size="30" class="red"></u--text> 		
							 {{status}}
						</u-form-item> -->
					</u--form>
					
					<template v-if="page=='edit'">
						<view class="hd-title m-t1">
							<view class="title">采购申请附件</view>	 
						</view>
						<view v-if="id">
							<c-uploadFile-cg module="caigou" :isUpload="send_user==userInfo.id?true:false" type='6' to_uid="" :procure_id="id"  v-if="hackreset"></c-uploadFile-cg>	 
						</view>
					</template>
					
					<view v-if="goods.list.length>0" v-for="(item,index) in goods.list" :key="index">
						<view class="hd-title">
							<view class="title">商品{{index+1}}</view>	
							<view v-if="page=='add'||page=='edit'" class="del" @click="delGoods(index)">删除</view> 
						</view>
						<u--form class="form-wrap" :model="form" ref="uForm">
							<u-form-item label="商品名称" labelWidth="160" prop="goods_name" borderBottom="true" class="u-form-item" @click="storehouse.goodsIndex=index;selectGoods('goods')">
								<u-input v-model="item.goods_name" disabled disabledColor="#ffffff" border="none" placeholder="选择采购商品" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>		
							</u-form-item>
							<u-form-item label="商品编号" labelWidth="160" prop="goods_number" borderBottom="true" class="u-form-item">
								<u-input v-model="item.goods_number" disabled disabledColor="#ffffff" border="none" placeholder="商品编号" style="pointer-events: none;"/> 		
							</u-form-item>							 
							<u-form-item label="数量"  prop="amount" labelWidth="160" borderBottom="true" class="u-form-item">
								<u-input type="digit" v-model="item.amount" disabledColor="#ffffff" border="none" placeholder="输入采购数量"  />
							</u-form-item> 
							<u-form-item label="入库仓库" labelWidth="160" prop="storehouse_name" borderBottom="true" class="u-form-item" @click="storehouse.goodsIndex=index;storehouse.isShow=true">
								<u-input v-model="item.storehouse_name" disabled disabledColor="#ffffff" border="none" placeholder="选择入库仓库" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>		
							</u-form-item> 
							<u-form-item label="供应商" labelWidth="160" prop="supplier_name" borderBottom="true" class="u-form-item">
								<u-input v-model="item.supplier_name" disabledColor="#ffffff" border="none" placeholder="输入供应商" /> 	
							</u-form-item> 
							<u-form-item label="备注" labelWidth="160" borderBottom="true" class="u-form-item"> 
								<u-input v-model="item.remark" disabledColor="#ffffff" border="none" placeholder="输入备注" />
							</u-form-item>
							 
						</u--form>
					</view> 				 
					<view class="add-btn" @click="createGoods">添加商品</view> 
				</template>
				
				
				<template v-if="page=='detail'">
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item v-if="page=='detail'&&status>2||(status==1&&job_id==7)||(status==2&&userInfo.department_id==44&&userInfo.dep_leader==1)" label="采购类型" labelWidth="160" prop="procure_type_name" borderBottom="true" class="u-form-item" @click="showType">
							<u-input v-model="form.procure_type_name" disabled disabledColor="#ffffff" border="none" placeholder="选择采购类型" style="pointer-events: none;"/>
							<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item label="采购单位" labelWidth="160" prop="company_val_name" borderBottom="true" class="u-form-item">
							<u--text :text="form.company_val_name" size="30" class="orange"></u--text> 	
						</u-form-item>
						<u-form-item label="申请原因或用途" labelWidth="300" prop="reason_purpose" borderBottom="true" class="u-form-item">
							<u--text :text="form.reason_purpose" size="30" class="orange"></u--text> 		
						</u-form-item>   
					</u--form>	
									
					<template>
						<view class="hd-title m-t1">
							<view class="title">申请原因或用途附件</view>	 
						</view>
						<view v-if="id">
							<c-uploadFile-cg module="caigou" :isUpload="true" type='6' to_uid="" :procure_id="id"  v-if="hackreset"></c-uploadFile-cg>	
						</view>
					</template>	
					
					<view v-if="goods.list.length>0" v-for="(item,index) in goods.list" :key="index">
						<view class="hd-title">
							<view class="title">商品{{index+1}}</view>	
							<view v-if="page=='add'||page=='edit'" class="del" @click="delGoods(index)">删除</view> 
						</view>
						<u--form class="form-wrap"> 
							<u-form-item label="商品名称" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="item.goods_name" size="30" class="orange"></u--text> 	 
							</u-form-item>
							<u-form-item label="商品编号" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="item.goods_number" size="30" class="orange"></u--text> 	 
							</u-form-item>							 
							<u-form-item label="数量" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="item.amount" size="30" class="orange"></u--text>  
							</u-form-item>
							<u-form-item label="入库仓库" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="item.storehouse_name" size="30" class="orange"></u--text>  
							</u-form-item>
							<u-form-item label="供应商" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="item.supplier_name" size="30" class="orange"></u--text>  
							</u-form-item>
							<!-- <u-form-item label="总价" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="item.total" size="30" class="orange"></u--text>  
							</u-form-item> -->
							<u-form-item label="备注" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="item.remark" size="30" class="orange"></u--text>  
							</u-form-item>
						</u--form>
					</view>
				</template> 			
				
				
				<template v-if="page=='detail'&&to_uid==userInfo.id&&((status==3||status==5)||(status==1&&job_id!=7))">
					<view class="hd-title">
						<view class="title">审批</view>	 
					</view>
					
					<u--form class="form-wrap">	
						<u-form-item label="审批意见" labelWidth="160" prop="is_agree_name" borderBottom="true" class="u-form-item" @click="shenpi.isShow=true">							
							<u-input v-model="shenpi.is_agree_name" disabled disabledColor="#ffffff" border="none" />
							<u-icon	slot="right" name="arrow-right"></u-icon>		
						</u-form-item>
						<u-form-item label="审批说明" labelWidth="250" prop="name" labelPosition="top" borderBottom="true" class="u-form-item">
							<u-input v-model="shenpi.remark" border="none" />
						</u-form-item> 
					</u--form>
				
				</template>
				
				<template v-if="page=='detail'&&shenpi_step.length>0">
					<view class="hd-title">
						<view class="title">审批记录</view>	 
					</view>
					<view class="flow-wrap m-b2">
						<!-- <view class="hd-title m-b4">
							<view class="title">流程</view> 
						</view> -->
						<view class="item" v-for="(item,index) in shenpi_step" :key="index">
							<view class="left-line">
								<view class="circle"></view>
								<view class="line"></view>
							</view>
							<view class="right">
								<view class="label">
									<view class="f-wb">{{item.username}}·{{item.status_name}}</view>
								</view>
								<view class="mail-user">
									<view class="left">
										<view v-if="true" class="user-item">
											<view class="avator">
												{{item.username|substringUsername}}
												<view v-if="item.is_agree==1" class="del-btn iconfont f-orange">&#xe637;</view>
												<view v-if="item.is_agree==2" class="del-btn iconfont f-orange">&#xe724;</view>
											</view> 
										</view> 
									</view>
									<view class="f-grey f-s28">
										<text v-if="item.is_agree==1">已同意·</text>
										<text v-if="item.is_agree==2">已驳回·</text>
										<text>{{item.create_time}}</text></view>
								</view>
							</view>
						</view>
						 
					</view>
					<!-- <view class="step-warp">
						<view class="step" v-for="(item,index) in shenpi_step" :key="index">
							<view class="num">{{index+1}}</view>
							<view class="info">
								<view class="content m-b2"><text>{{item.content}}</text><text>{{item.create_time}}</text></view>
								<view v-if="item.remark" class="remark">审批说明：{{item.remark}}</view> 
							</view>
						</view>
					</view> -->
				</template>
			 
				
				<template v-if="page=='detail'&&is_agree!=0">
					<view class="hd-title">
						<view class="title">审批结果</view>	 
					</view>
					
					<u--form class="form-wrap">	
						<u-form-item label="审批意见" labelWidth="160" prop="is_agree_name" borderBottom="true" class="u-form-item">
							<u--text :text="shenpi.is_agree_name" size="30" class="red"></u--text>	
						</u-form-item>
						<u-form-item label="审批说明" labelWidth="160" prop="name" borderBottom="true" class="u-form-item">
							<u--text :text="shenpi.remark_detail" size="30"/></u--text>
						</u-form-item> 
					</u--form>
				
				</template>
				<!--  -->
				<template v-if="page=='detail'&&status>2||(status==1&&job_id==7)||(status==2&&userInfo.department_id==44&&userInfo.dep_leader==1)" class="m-b2">
					<view class="hd-title">
						<view class="title">采购计划</view>	 
					</view>
					<view v-if="goods.list.length>0" v-for="(sitem,sindex) in goods.list" :key="sitem.id" class="m-b2">
						<view class="hd-title">
							<view class="title">商品{{sindex+1}}</view>	 
						</view>
						<u--form class="form-wrap"> 
							<u-form-item label="商品名称" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="sitem.goods_name" size="30" class="orange"></u--text> 	 
							</u-form-item>
							<u-form-item label="商品编号" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="sitem.goods_number" size="30" class="orange"></u--text> 	 
							</u-form-item>							 
							<u-form-item label="数量" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="sitem.amount" size="30" class="orange"></u--text>  
							</u-form-item>
							<u-form-item label="入库仓库" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="sitem.storehouse_name" size="30" class="orange"></u--text>  
							</u-form-item>
							<u-form-item label="供应商" labelWidth="160" prop="cg_supplier_name" borderBottom="true" class="u-form-item" @click="spsupplierlistsel.goodsIndex=sindex;spsupplierlistsel.isShow=true;">
								<template v-if="status==2">
									<u-input v-model="sitem.cg_supplier_name" disabled placeholder="选择供应商" disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
									<u-icon	slot="right" name="arrow-right"></u-icon>	
								</template>
								<template v-else>
									<u--text :text="sitem.cg_supplier_name" size="30" class="orange"></u--text>  
								</template>	
							</u-form-item>
							<u-form-item label="单价" labelWidth="160" prop="price" borderBottom="true" class="u-form-item">
								<u-input type="digit" v-model="sitem.price" placeholder="输入单价" disabledColor="#ffffff" border="none" />
							</u-form-item>
							<u-form-item label="总计" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text v-if="sitem.price*sitem.amount>0" :text="(sitem.price*sitem.amount).toFixed(2)" size="30" class="orange"></u--text>  
							</u-form-item>
							<u-form-item label="申请时间" labelWidth="160" borderBottom="true" class="u-form-item">
								<u--text :text="sitem.create_time" size="30" class="orange"></u--text>  
							</u-form-item>
						</u--form>
					</view>
					<view v-if="id">
						<view class="hd-title">
							<view class="title">采购计划附件</view>	 
						</view>
						<c-uploadFile-cg module="caigou" :isUpload="userInfo.department_id==44&&userInfo.dep_leader==1&&((detail.status==1&&job_id==7)||detail.status==2||detail.status==3||(detail.status==4&&(is_agree==0||is_agree==2)))?true:false" type='4' :procure_id="id"  v-if="hackreset"></c-uploadFile-cg>	
					</view>
				</template>			
				
				<template v-if="page=='detail'&&detail.status==3&&to_uid==userInfo.id">
					<u--form class="form-wrap m-t2">
						<u-form-item label="是否上传合同" labelWidth="260" prop="hetong.need_contract_name" borderBottom="true" class="u-form-item" @click="hetong.isShow=true">
							<u-input v-model="hetong.need_contract_name" disabled disabledColor="#ffffff" border="none" />
							<u-icon	slot="right" name="arrow-right"></u-icon>		
						</u-form-item> 
					</u--form>
					 
				</template>
				
				<template v-if="page=='detail'&&(detail.status>4||(detail.status==4&&userInfo.department_id==44))&&detail.need_contract==1">
					<view class="hd-title">
						<view class="title">采购合同</view>	 
					</view>
					<view v-if="id">
						<c-uploadFile-cg module="caigou" :isUpload="userInfo.department_id==44&&userInfo.dep_leader==1" type='1' :procure_id="id"  v-if="hackreset"></c-uploadFile-cg>	
					</view>
				</template>	 
				
				<template v-if="page=='detail'&&(detail.status>=8)"> 			 <!-- ||(detail.status==8&&userInfo.department_id==31)		 -->
					<template>
						<view class="hd-title">
							<view class="title">货款核销</view>	 
						</view>
						<view v-if="id">
							<!-- userInfo.department_id==31&&userInfo.dep_leader==1&&detail.status==8?true:false -->
							<c-uploadFile-cg module="caigou" :isUpload="detail.status==8?true:false" type='5' :procure_id="id"  v-if="hackreset"></c-uploadFile-cg>	
						</view>
					</template>
				</template>
				 
			</view>
			
			<u-picker :show="storehouse.isShow" :columns="storehouse.data" keyName="sname" :defaultIndex="storehouse.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="storehouse.isShow=false" @confirm="changestorehouse" @close="storehouse.isShow=false"></u-picker>
			<u-picker :show="spsupplierlistsel.isShow" :columns="spsupplierlistsel.data" keyName="name" :defaultIndex="spsupplierlistsel.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="spsupplierlistsel.isShow=false" @confirm="changespsupplierlistsel" @close="spsupplierlistsel.isShow=false"></u-picker>
			<u-picker :show="level.isShow" :columns="level.data" keyName="label" :defaultIndex="level.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="level.isShow=false" @confirm="changeLevel" @close="level.isShow=false"></u-picker>
			<u-picker :show="type.isShow" :columns="type.data" keyName="label" :defaultIndex="type.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="type.isShow=false" @confirm="changeType" @close="type.isShow=false"></u-picker>
			<u-picker :show="mode.isShow" :columns="mode.data" keyName="label" :defaultIndex="mode.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="mode.isShow=false" @confirm="changeMode" @close="mode.isShow=false"></u-picker>
			<u-picker :show="purpose.isShow" :columns="purpose.data" keyName="label" :defaultIndex="purpose.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="purpose.isShow=false" @confirm="changePurpose" @close="purpose.isShow=false"></u-picker>
			<u-picker :show="shenpi.isShow" :columns="shenpi.agree_option" keyName="label" :defaultIndex="shenpi.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="shenpi.isShow=false" @confirm="changeShenpi" @close="shenpi.isShow=false"></u-picker>			
			<u-picker :show="hetong.isShow" :columns="hetong.data" keyName="label" :defaultIndex="hetong.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="hetong.isShow=false" @confirm="changehetong" @close="hetong.isShow=false"></u-picker> 
			<u-picker :show="company.isShow" :columns="company.data" keyName="label" :defaultIndex="company.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="company.isShow=false" @confirm="changecompany" @close="company.isShow=false"></u-picker> 
		</view> 
		
		 
		<view v-if="page=='detail'&&(status==1&&job_id==7)||(status==2&&userInfo.department_id==44&&userInfo.dep_leader==1)" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<view class="btn submit-btn" @click="addPlan">提交采购计划</view> 
			</view>
		</view> 
		
		<view v-if="page=='add'||page=='edit'" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<view class="btn submit-btn" @click="addGoods">{{id?'保存修改':'提交申请'}}</view> 
			</view>
		</view>
		
		<view v-if="page=='detail'&&to_uid==userInfo.id&&status==1&&job_id!=7" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<view class="btn submit-btn" @click="procureApprover(2)">采购立项审批</view> 
			</view>
		</view>
		
		<view v-if="to_uid==userInfo.id&&status==3" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<view class="btn submit-btn" @click="procureApprover(4)">采购计划审批</view> 
			</view>
		</view>
		
		<view v-if="to_uid==userInfo.id&&status==5" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<view class="btn submit-btn" @click="procureApprover(6)">采购合同审批</view> 
			</view>
		</view>
		
		<view v-if="to_uid==userInfo.id&&((status==4&&need_contract==0)||status==6)&&is_agree==1" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<view class="btn submit-btn" @click="procureApprover(7)">采购中</view> 
			</view>
		</view>
		
		<view v-if="to_uid==userInfo.id&&status==7" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<view class="btn submit-btn" @click="store">验收入库</view> 
			</view>
		</view>
		
		<view v-if="to_uid==userInfo.id&&status==9" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<view class="btn submit-btn" @click="procureApprover(10)">结束</view> 
			</view>
		</view>
		  
		 
		
		<view v-if="is_agree==2&&send_user==userInfo.id&&page=='detail'" class="footer">
			<view class="fix-wrap created-btn-wrap">
				<navigator :url="'/package_caigou/created_step-1/created_step-1?type=edit&id='+id" hover-class="none" class="btn submit-btn">修改申请</navigator> 
			</view>
		</view>
		
	</view>
</template>

<script>
	import {
			checkPhone
		} from '@/common/util.js'
	export default {
		data() {
			return {
				page:'',
				id:'',
				to_uid:'', 
				send_user:'',
				user_id:'',
				job_id:'',
				msg_type:'',
				userInfo:uni.getStorageSync('userInfo'),
				status:'',
				is_agree:'',
				agree_user_id:'',
				remark:'',
				hackreset:true,
				detail:{},
				form: {
					procure_id:'',
					procure_type:'',
					procure_type_name:'',
					reason_purpose:'',
					reason_purpose_name:'',
					company_val:'',
					company_val_name:'',
					department_id:uni.getStorageSync('userInfo').department_id,					
					procure_num:'',					
					user_name:uni.getStorageSync('userInfo').username, 					
					procure_level:'1',
					procure_level_name:'一般',					
					procure_mode:'1',
					procure_mode_name:'单一来源',
					procure_company:'',
					procure_company_name:'',
					f_ids:''
				},
				rules: {
					user_name: [
						{
							required: true,
							message: '请输入姓名',
							trigger: ['blur', 'change']
						}
					],
					department_id:[
						{
							required: true,
							message: '请输入申请部门',
							trigger: ['blur', 'change']
						}
					],
					procure_level_name:[
						{
							required: true,
							message: '请输入紧急程度',
							trigger: ['blur', 'change']
						}
					],
					procure_type_name:[
						{
							required: true,
							message: '请输入采购类型',
							trigger: ['blur', 'change']
						}
					]
				},
				
				level:{
					isShow:false,
					data:[
						[
							{
								label: '一般', 
								id: 1 
							}, 
							{
								label: '重要',
								id: 2
							},
							{
								label: '紧急',
								id: 3
							}
						],
					],
					defaultIndex:[]
				},
				type:{
					isChange:true,
					isShow:false,
					data:[
						[
							{
								label: '不限', 
								id: 0 
							}, 
							{
								label: '实物类', 
								id: 1 
							}, 
							{
								label: '服务类',
								id: 2
							} 
						],
					],
					defaultIndex:[]
				},
				mode:{ 
					isShow:false,
					data:[
						[
							{
								label: '单一来源', 
								id: 1 
							}, 
							{
								label: '竞争性谈判',
								id: 2
							},
							{
								label: '询价',
								id: 3
							},
							{
								label: '招标',
								id: 4
							} 
						],
					],
					defaultIndex:[]
				},	
				company:{
					isShow:false,
					data:[
						[
							 
						],
					],
					defaultIndex:[]
				},
				purpose:{ 
					isShow:false,
					data:[
						[],
					],
					defaultIndex:[]
				},
				 
				goods:{
					num:-1,
					list:[ 
						// {
						// 	goods_number: "",
						// 	goods_name: "",    
						// 	amount: "",
						// 	supplier_name: "",
						// 	storehouseid: "",
						// 	storehouse_name:'',
						// 	remark: ""
						//  cg_supplier_id:'',
						//  cg_supplier_name:'',
						// } 
					]
				},
				hetong:{
					isShow:false,
					need_contract:0,
					need_contract_name:'否',
					data:[
						[
							{
								label:'否',
								id:0
							},
							{
								label:'是',
								id:1
							}
						]						
					]
				},
				storehouse:{
					goodsIndex:'',
					isShow:false, 
					data:[[]],
					defaultIndex:[]
				},
				spsupplierlistsel:{
					goodsIndex:'',
					isShow:false, 
					data:[[]],
					defaultIndex:[]
				},
				shenpi:{
					isShow:false,
					procure_id:'',
					status:'',
					is_agree:'0',
					is_agree_name:'待审批',
					remark:'',
					remark_detail:'',
					agree_option:[
						[
							{
								label: '待审批', 
								id: 0 
							}, 
							{
								label: '通过',
								id: 1
							},
							{
								label: '未通过',
								id: 2
							} 
						],
					],
					defaultIndex:[]
				},
				isStop:true,
				loadingState:{
					default:true
				},
				isCaigou:false,
				need_contract:0,
				shenpi_step:[],
				totalprice:0
			}
		},
		onReady() {
				//onReady 为uni-app支持的生命周期之一
		    	// this.$refs.uForm.setRules(this.rules)
		},
		filters: {
			substringUsername(val) {
				if (val) return val.substring(val.length - 2);
			},
		},
		async onLoad(params) { 
			console.log(params)
			this.isLogin=await this.$loginState('check');
			this.id=params.id; 
			this.page=params.type;
			// this.to_uid=params.to_uid;
			this.msg_type=params.msg_type;
			
			if(this.page=='edit'){
				uni.setNavigationBarTitle({
					title:'编辑采购'
				})
			}else if(this.page=='add'){
				uni.setNavigationBarTitle({
					title:'采购申请'
				})
			}else if(this.page=='detail'||this.page=='shenpi'){
				uni.setNavigationBarTitle({
					title:'采购详情'
				})
			} 
			
			if(this.isLogin){ 				 
				this.companylistsel();
				this.productstorehouse(); 
				if(this.page=='add'){					
					
				} 
				if(this.page=='edit' || this.page=='detail'){
					this.getprocureInfo();  
					this.procuremsgtrace();
					this.procuretracelist();
					this.getspsupplierlistsel();
					
				} 
			}
			  
		},
		methods: {
			addPlan(){
				if(this.form.procure_type=='0'){
					this.$showToast({
						title:'请选择采购类型',
						duration:1000,
					})
					return
				}
				let goods=[];
				for(let i in this.goods.list){
					if(this.goods.list[i].cg_supplier_id=='' || this.goods.list[i].price== '') {
						this.$showToast({
							title:'请填写完整商品信息在提交',
							duration:1000,
						})  						  
						return false
					}
					if(this.goods.list[i].price<=0){
						this.$showToast({
							title:'数量必须大于0',
							duration:1000,
						})  						  
						return false
					}
					let item= {}
					item.id=this.goods.list[i].id;
					item.cg_supplier_id=this.goods.list[i].cg_supplier_id;
					item.price=this.goods.list[i].price; 
					goods.push(item)
				}  
				if(this.form.f_ids==''){
					this.$showToast({
						title:'请上传采购计划',
						duration:1000,
					})
					return
				} 
				uni.showLoading({
					title:'提交中'
				}) 
				if(this.isStop){
					this.isStop=false;
					this.$api.user_api.procureplan({
						procure_id:this.id,
						f_ids:this.form.f_ids,
						goods:JSON.stringify(goods)
					}).then(res=>{
						if(res.data.code==200){ 
							this.$store.commit('CAIGOU_LIST_UPDATE',1);
							this.$showToast({
								icon:'success',
								title:res.data.msg,
								duration:1000,
							}).then(()=>{
								uni.navigateBack({
									delta:1
								})						 
							})							
							this.isStop=true;
						}else{
							this.isStop=true;
							this.$showToast({
								icon:'error',
								title:res.data.msg,
								duration:1000,
							})  	 
						} 
					})
				}
				
			},
			
			showCompany(){
				this.company.isShow=true;
			},
			
			async productstorehouse(){
				let res=await this.$api.user_api.productstorehouse(); 
				if(res.data.code==200){
					this.$set(this.storehouse.data,0,res.data.data) 
				}
			},
			changestorehouse(e){
				this.goods.list[this.storehouse.goodsIndex].storehouseid=e.value[0].id;
				this.goods.list[this.storehouse.goodsIndex].storehouse_name=e.value[0].sname;
				this.storehouse.isShow=false;
			},
			selectGoods(){ 
				uni.navigateTo({
					url:'/package_kucun/list-select/list-select?page=goods&index='+this.storehouse.goodsIndex
				})
			},
			changecompany(e){
				this.company.defaultIndex=e.indexs;
				this.form.company_val=e.value[0].value;
				this.form.company_val_name=e.value[0].label;
				this.company.isShow=false;
			},
			async companylistsel(){
				let res=await this.$api.user_api.companylistsel(); 
				if(res.data.code==200){
					res.data.data.forEach(item=>{
						this.company.data[0].push(item)
					})  
				}
			},
			async getspsupplierlistsel(){ 
				let res=await this.$api.user_api.spsupplierlistsel(); 
				console.log(res)
				if(res.data.code==200){
					this.$set(this.spsupplierlistsel.data,0,res.data.data)   
				}
			},
			changespsupplierlistsel(e){
				this.goods.list[this.spsupplierlistsel.goodsIndex].cg_supplier_id=e.value[0].id;
				this.goods.list[this.spsupplierlistsel.goodsIndex].cg_supplier_name=e.value[0].name;
				this.spsupplierlistsel.isShow=false;
			},
			
			async procuretracelist(){
				let res=await this.$api.user_api.procuretracelist({
					procure_id:this.id
				})   
				console.log(res)
				if(res.data.code==200){
					res.data.data.list.forEach(item=>{
						item.content= item.username+(item.is_agree==1?'同意':'')+(item.is_agree==2?'已驳回':'')+item.status_name 
					})
					this.shenpi_step=res.data.data.list;
				}
			},
			
			async store(){
				let res=await this.$api.user_api.procuredetailstore({
					procure_id:this.id,
					goods:JSON.stringify(this.goods.list)
				}) 
				if(res.data.code==200){
					this.$store.commit('CAIGOU_LIST_UPDATE',1);
					this.$showToast({
						title:res.data.msg,
						duration:1000,
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta:1
						})
					},1000)
				}
			},
			
			async procuremsgtrace(){
				let res=await this.$api.user_api.procuremsgtrace({
					procure_id:this.id
				})  
				if(res.data.code==200){
					this.is_agree=res.data.data.is_agree;						
					this.agree_user_id=res.data.data.agree_user_id;
					this.shenpi.status=res.data.data.status;
					this.shenpi.remark_detail=res.data.data.remark;					
					this.shenpi.agree_option[0].forEach(item=>{
						if(item.id==res.data.data.is_agree){
							this.shenpi.is_agree_name=item.label;
						}
					})
				}
			},
			
			changehetong(e){
				this.hetong.defaultIndex=e.indexs;
				this.hetong.need_contract=e.value[0].id;
				this.hetong.need_contract_name=e.value[0].label;
				this.hetong.isShow=false;
				this.need_contract=e.value[0].id;
			},
			
			showLevel(){
				this.level.isShow=true;
			},
			changeLevel(e){ 
				this.level.defaultIndex=e.indexs;
				this.form.procure_level=e.value[0].id;
				this.form.procure_level_name=e.value[0].label;
				this.level.isShow=false;
			},
			
			showType(){ 
				if(!this.type.isChange&&this.detail.status>2) return;
				this.type.isShow=true;
			},
			changeType(e){ 
				this.type.defaultIndex=e.indexs;
				this.form.procure_type=e.value[0].id;
				this.form.procure_type_name=e.value[0].label; 
				this.type.isShow=false;
				// 实物类：1=商品，2=原材料，3=设备，4=固定资产，5=其他)(服务类：1=维修，2=保养，3=评估，4=咨询，5=其他)
				 
				if(this.form.procure_type==1){
					this.purpose.data=[
						[
							{
								label: '商品', 
								id: 1 
							}, 
							{
								label: '原材料',
								id: 2
							},
							{
								label: '设备',
								id: 3
							},
							{
								label: '固定资产',
								id: 4
							},
							{
								label: '其他',
								id: 5
							} 
						],
					]; 
				}
				
				if(this.form.procure_type==2){
					this.purpose.data=[
						[
							{
								label: '维修', 
								id: 1 
							}, 
							{
								label: '保养',
								id: 2
							},
							{
								label: '评估',
								id: 3
							},
							{
								label: '咨询',
								id: 4
							},
							{
								label: '其他',
								id: 5
							} 
						],
					];					 
				}
				
				this.$api.user_api.procureupdata({
					id:this.id,
					procure_type:this.form.procure_type
				}).then(res=>{
					console.log(res)
				})
			},
			
			showMode(){ 
				this.mode.isShow=true;
			},
			changeMode(e){ 
				this.mode.defaultIndex=e.indexs;
				this.form.procure_mode=e.value[0].id;
				this.form.procure_mode_name=e.value[0].label;
				this.mode.isShow=false;
			},
			
			showPurpose(){
				this.purpose.isShow=true;
			},
			changePurpose(e){ 
				this.purpose.defaultIndex=e.indexs;
				this.form.reason_purpose=e.value[0].id;
				this.form.reason_purpose_name=e.value[0].label;
				this.purpose.isShow=false;
				if(this.form.reason_purpose==5){
					
				}
			},
			
			createGoods(){				
				this.goods.num += 1; 
				this.$set(this.goods.list,this.goods.num, {  
					goods_id:'',
					goods_number: "",
					goods_name: "",    
					amount: "",
					supplier_name: "",
					storehouseid: "",
					storehouse_name:'',
					remark: "" 
				})  
			},
			
			async delGoods(idx){
				let id=this.goods.list[idx].id;
				if(this.page=='add'){
					this.goods.num -= 1;
					this.goods.list.splice(idx, 1);
				}
				if(this.page=='edit'){
					if(id){
						let res=await this.$api.user_api.procuredetailDelgoods({
							id:id
						}) 
						if(res.data.code==200){
							this.goods.num -= 1;
							this.goods.list.splice(idx, 1);							 
						}
					}else{ 
						this.goods.num -= 1;
						this.goods.list.splice(idx, 1);
					}					
				}
				
			},
			
			// 获取详情
			async getprocureInfo(){
				let res=await this.$api.user_api.procureInfo({
					id:this.id 
				})  
				console.log(res.data.data)
				if(res.data.code==200){  
					this.detail=res.data.data;
					this.to_uid=this.detail.approver_uid;
					this.status=this.detail.status;
					this.send_user=this.detail.user_id;
					this.form.company_val=this.detail.company_val;
					this.form.company_val_name=this.detail.company_name;
					this.form.reason_purpose=this.detail.reason_purpose;
					this.form.procure_id=this.detail.id;
					if(this.detail.procure_type){
						this.form.procure_type=this.detail.procure_type;
						this.form.procure_type_name=this.detail.type_name;						
						this.type.isChange=false;
					}
					
					this.detail.goods.forEach(i=>{
						let item={};
						item.goods_id=i.goods_id;
						item.id=i.id;
						item.goods_number=i.goods_number;
						item.goods_name=i.name;  
						item.amount=i.amount;
						item.supplier_name=i.supplier_name;
						item.storehouseid=i.storehouseid;
						item.storehouse_name=i.storehouse_name;
						item.remark=i.remark;
						item.cg_supplier_id=i.cg_supplier_id;
						item.cg_supplier_name=i.cg_supplier_name;
						item.price=i.price>0?i.price:'';
						item.create_time=i.create_time;
						this.goods.list.push(item);
					})
					this.goods.num=this.detail.goods.length-1;
					 
				}
			},
			
			async procurestepres(){
				let res=await this.$api.user_api.procurestepres({
					status:Number(this.status)+1
				})    
				if(res.data.code==200){
					if(res.data.data.step&&(res.data.data.step==3||res.data.data.step==5||res.data.data.step==7||res.data.data.step==8)){
						 
						this.isCaigou=true;
					}
				}
			},
			
			async addGoods(){
				
				if(this.goods.list.length==0){
					this.$showToast({
						title:'请添加采购商品',
						duration:1000,
					})  						  
					return false
				}
				let goods=[];
				for(let i in this.goods.list){
					if(this.goods.list[i].goods_name=='' || this.goods.list[i].amount== ''||this.goods.list[i].storehouseid== ''||this.goods.list[i].supplier_name== '') {
						this.$showToast({
							title:'请填写完整商品信息在提交',
							duration:1000,
						})  						  
						return false
					}
					if(this.goods.list[i].amount<=0){
						this.$showToast({
							title:'数量必须大于0',
							duration:1000,
						})  						  
						return false
					}
					let item= {}
					item.goods_id=this.goods.list[i].goods_id;
					item.name=this.goods.list[i].goods_name;
					item.goods_number=this.goods.list[i].goods_number;
					item.amount=this.goods.list[i].amount;
					item.supplier_name=this.goods.list[i].supplier_name;
					item.storehouseid=this.goods.list[i].storehouseid;
					item.remark=this.goods.list[i].remark;
					goods.push(item)
				}  
				if(this.isStop){
					this.isStop=false;
					let {procure_type,reason_purpose,company_val,department_id}=this.form;  
					let res=null;
					if(this.id){
						res=await this.$api.user_api.procureEdit({
							id:this.id,
							procure_type,
							procure_level,
							reason_purpose:reason_purpose_name,
							procure_mode, 
							goods:JSON.stringify(this.goods.list)
						});  
					}else{
						res=await this.$api.user_api.procureAdd({	
							id:'',
							procure_type,
							reason_purpose,
							company_val,
							department_id,
							goods:JSON.stringify(goods)
						});  
					} 
					if(res.data.code==200){ 
						if(res.data.data){
							this.id=res.data.data.procure_id;
						}
						this.$store.commit('CAIGOU_LIST_UPDATE',1);
						this.$showToast({
							icon:'success',
							title:res.data.msg,
							duration:1000,
						}).then(()=>{
							uni.navigateBack({
								delta:1
							})						 
						})
						
						this.isStop=true;
					}else{
						this.isStop=true;
						this.$showToast({
							icon:'error',
							title:res.data.msg,
							duration:1000,
						})  	 
					} 
				}
				
				
			},
			
			changeShenpi(e){
				this.shenpi.defaultIndex=e.indexs;
				this.shenpi.is_agree=e.value[0].id;
				this.shenpi.is_agree_name=e.value[0].label;
				this.shenpi.isShow=false;
			}, 
			
			
			
			async procureApprover(sta){				
				let {procure_id,is_agree,remark}=this.shenpi; 			
				
				if(is_agree==0&&(this.status==1||this.status==3)){
					this.$showToast({ 
						title:'请选择审批意见',
						duration:1000,
					})  
					return
				} 
				let params={};
				if(sta==4){
					params={
						procure_id:this.id,
						status:sta,
						is_agree,
						remark,
						need_contract:this.need_contract
					}
					
				}else{
					params={
						procure_id:this.id,
						status:sta,
						is_agree,
						remark,
					}						
				} 
				let res=await this.$api.user_api.procureApprover(params); 
				  
				if(res.data.code==200){
					this.$store.commit('CAIGOU_LIST_UPDATE',1);
					this.$showToast({
						icon:'success',
						title:res.data.msg,
						duration:1000,
					}).then(()=>{
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					})
				} 
			}
			
			
		},
		
		computed:{
			goods_update(){
				return this.$store.state.goods_update
			},
			update(){
				return this.$store.state.caigou_list
			},
			files_update(){
				return this.$store.state.caigou_files;
			}
			 
		},
		watch:{
			update(newer,older){   
				if(newer!=older){	
					this.goods.list=[];
					setTimeout(()=>{
						this.getprocureInfo();
						this.procuremsgtrace();
					},30) 
				}
			},
			goods_update(newer,older){
				if(newer!=older){  
					this.goods.list[newer.index].goods_name=newer.data.pname;
					this.goods.list[newer.index].goods_number=newer.data.p_number; 
					this.goods.list[newer.index].goods_id=newer.data.id;
				}
			},
			files_update(newer,older){
				if(newer!=older){ 
					this.form.f_ids=newer.id;
				}
			}
			
			 
		}
	}
</script>

<style lang="less" scoped>
	.step-warp{
		background: #fff;
		.step{
			display: flex;
			font-size: 30rpx;
			padding:  20rpx;
			border-bottom: 1rpx solid #eee;
			.num{
				width: 50rpx;
				flex-shrink: 0;
			}
		}
		.info{
			flex-grow: 2;
		}
		.content{
			display: flex;
			justify-content: space-between;
		}
	}
	.form-wrap{
		.u-form-item{
			padding-left: 20rpx;
		}
	}
	.flow-wrap{
		padding:30rpx 0;
		background: #fff;
		.hd-title{
			display: flex;
			justify-content: space-between;
			margin:0 20rpx 20rpx;		
			.title{
				padding-left:20rpx;
				border-left: 8rpx solid #0099ff;
				font-size: 30rpx;
				font-weight: bold;
			}
			
		}
	}
	.flow-wrap .item{
		display: flex;
		padding:0 20rpx; 
	}
	.flow-wrap .item .left-line{
		width: 30rpx;
		margin: 10rpx 20rpx 0 0;
	}
	.flow-wrap .item .left-line .circle{
		width: 19rpx;
		height: 19rpx;
		background: #ccc;
		border-radius: 50%;
	}
	.flow-wrap .item .left-line .line{ 
		height:150rpx;
		margin: 9rpx 0 0 9rpx;
		border-left: 1rpx solid #ccc;
		flex-shrink: 0;
	}
	.flow-wrap .item .right{
		flex-grow: 2; 
	}
	.flow-wrap .item .right .label{
		font-size: 32rpx; 
		margin-bottom: 30rpx;
	}
	.flow-wrap .item .right .mail-user{
		display: flex;  
		align-items: center;
		justify-content: space-between;
		flex-grow: 2;
	}
	.flow-wrap .item .right .mail-user .left{
		display: flex;
		align-items: center; 
	}
	.flow-wrap .item .right .mail-user .add{
		margin: 12rpx 20rpx;
		font-size: 32rpx;
	}
	.flow-wrap .item .right .user-item{
		text-align: center;
		font-size: 28rpx;
	}
	.flow-wrap .item .right .mail-user .add-user-btn{
		width:70rpx;
		margin-left: 20rpx;
	}
	.flow-wrap .item .right .mail-user .add-user-btn image{
		width: 100%;
	}
	
	.project-form-wrap{
		padding: 20rpx 0 0;
		background: #fff;
	}
	.hd-title{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding:  20rpx;
		.title{ 
			font-weight: bold;
			flex-shrink: 0;
			font-size: 32rpx;
		}
		.del{
			font-size: 32rpx;
			color: #f00;
		}
		
	}
	.add-btn{ 
		height: 80rpx;
		line-height: 80rpx;  
		margin: 20rpx;
		background: #fff;
		border:2rpx solid #0099ff; 
		border-radius: 8rpx;
		text-align: center;
		font-size: 30rpx;
		color:#0099ff;
	}
	
	.created-btn-wrap{
		display: flex;
		.btn{
			height: 80rpx;
			line-height: 80rpx;
			margin: 10rpx 10rpx 0;
			flex-grow: 1;
			border-radius: 8rpx;
			text-align: center;
			font-size: 30rpx;
			color:#fff;
		}
		.next-btn{
			background: #ff9600; 
		}
		.submit-btn{
			background: #0099ff; 
		}
	} 
	.step-bar{
		height: 90rpx;
		margin-bottom: 20rpx;
		.fix-wrap{
			display: flex;
			height: 90rpx;
			background: #fff;
		}
		.item{
			flex-grow: 1;
			width: 25%;
			text-align: center;
			color:#999;
			.text{
				font-size: 28rpx;				
			}
			.icon{
				width: 40rpx;
				height: 40rpx;
				margin:0 auto 0; 
			}
		}
		.active{
			.text,.icon{
				color:#09f;				
			} 
		}
	}
</style>
